//
//  CQTabBar.swift
//  weibo
//
//  Created by chengqiang on 16/7/18.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit

class CQTabBar: UITabBar {
    
    var  composeBack:(()->Void)?
    
  ///添加加号按钮
    lazy var composeButton:UIButton={
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
            
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(button)
        return button
        
    }()
    
    let ItemCount:Int = 5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let itemwidth = self.frame.width / CGFloat(ItemCount)
        var index = 0
        
        for item in self.subviews {
            
            
            ///判断子空间是否是TabBar类
            let cls = NSClassFromString("UITabBarButton")!
            if item.isKindOfClass(cls)
            {
                item.frame=CGRect(x: CGFloat(index)*itemwidth, y: 0, width: itemwidth, height: self.frame.height)
                
                index++
                if index==2
                {
                    composeButton.frame=CGRect(x: 2*itemwidth, y: 0, width: itemwidth, height: self.frame.height)
                    index++
                }
            }
        }
    }
    func composeClick() {
        composeBack?()
    }
    

}
